package io.geekidea.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.springboot.entity.BuildingType;
import io.geekidea.springboot.exception.BusinessException;
import io.geekidea.springboot.mapper.BuildingMapper;
import io.geekidea.springboot.mapper.BuildingTypeMapper;
import io.geekidea.springboot.service.IBuildingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BuildingTypeServiceImpl extends ServiceImpl<BuildingTypeMapper, BuildingType> implements IBuildingTypeService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public boolean save(BuildingType buildingType) {
        buildingType.setCreatedAt(LocalDateTime.now());
        buildingType.setUpdatedAt(LocalDateTime.now());
        return super.save(buildingType);
    }

    @Override
    public boolean delete(Long id) {
        //检查该类型下是否有设施
        int count = buildingMapper.countByTypeId(id);
        if (count > 0) {
            throw new BusinessException(400,"该设施类型已被使用，无法删除");
        }
        return super.removeById(id);
    }


    @Override
    public boolean updateById(BuildingType buildingType) {
        buildingType.setUpdatedAt(LocalDateTime.now());
        return super.updateById(buildingType);
    }
}
